const util = require('../../utils/util');
const config = require('../../common/config');


Page({
    data: {
        loginUserIcon: 'https://www.zgyqsh.cn/images/icons/user_2.png',
        isLoading: false,
        isDisabled: true,
        account: '',
        password: '',
        canRegist:true
    },
    toRegistPage: function () {
        wx.navigateTo({
            url: '../../pages/regist/index'
        })
    },
    canButtonUse: function () {
        if (this.data.account == '' || this.data.password == '') {
            this.setData({
                isDisabled: true
            })
        } else {
            this.setData({
                isDisabled: false
            })
        }
    },
    doLogin: function (e) {
        var _this = this;
        this.setData({
            isLoading: true,
            isDisabled: true
        })
        wx.showLoading({
            title: '登录中',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask:true
        })


        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'wxMiniAppService',
                methodName: 'loginSelf',
                reqData:JSON.stringify({account:this.data.account,password:this.data.password})
            },
            success: function (res) {
                var resultData = res.data;
                wx.hideLoading();
                _this.setData({
                    isLoading: false,
                    isDisabled: false
                })
                if(resultData.code == 1){
                    util.debug('loginjs:通过账号密码系统登录成功');
                    wx.setStorageSync(config.USER_ID,resultData.data.userId);
                    wx.reLaunch({
                        url: '../../pages/home/index'
                    })
                }else{
                    util.debug('loginjs:通过账号密码系统登录失败');
                    util.debug(resultData);
                }
            },
            fail:function (e) {
                wx.hideLoading();
                _this.setData({
                    isLoading: false,
                    isDisabled: false
                })
                util.debug(e.errMsg);
            }
        })

    },
    bindAccountChange: function (e) {
        this.setData({
            account: e.detail.value
        })
        this.canButtonUse();
    },
    bindPasswordChange: function (e) {
        this.setData({
            password: e.detail.value
        })
        this.canButtonUse();
    }
})